/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servinf.ani.sincronizador.reader;

import com.servinf.ani.sincronizador.exception.ExcelReaderException;
import com.servinf.ani.sincronizador.utils.LogHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author mortiz
 */
public class ExcelReader {
    
    
    public static final int PROJECT_SHEET_INDEX = 0;
    public static final int ASSIGNMENT_SHEET_INDEX = 1;
    public static final int CONCESSSIONS_SHEET_INDEX = 2;
    
    public List<List<Object>> read(String path, int sheetIndex) throws FileNotFoundException, ExcelReaderException {
        try {
            LogHelper.Log(LogHelper.INFO, "Leyendo el archivo excel en la ruta " + path + " en hoja " + sheetIndex, null);
            List<List<Object>> rowInformation = new ArrayList<List<Object>>();
            File excelFile = new File(path);
            FileInputStream fileInputStream = new FileInputStream(excelFile);
            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext())        {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();
                List<Object> cells = new ArrayList<Object>();
                while (cellIterator.hasNext())   {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BLANK:
                            //System.out.println(" <> |");
                            cells.add(null);
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            //System.out.print(cell.getNumericCellValue() + " | ");
                            cells.add(new Double(cell.getNumericCellValue()));
                            break;
                        case Cell.CELL_TYPE_STRING:
                            //Cadena que usualmente está presente en el archivo excel para erepresentar cadenas en blanco
                            if(cell.getStringCellValue().equals("(en blanco)")) { 
                                cells.add(null);                                
                            }else {
                                cells.add(cell.getStringCellValue());
                            }
                            //System.out.print(cell.getStringCellValue() + " | ");
                            break;
                    }
                }
                rowInformation.add(cells);
            }
            fileInputStream.close();
            LogHelper.Log(LogHelper.INFO, "Archivo leído satisfactoriamente " + path, null);
            return rowInformation;
        }catch(IOException ioe){
           throw new ExcelReaderException("Error leyendo archivo excel " + ioe.getMessage());
        }
    }
    
    
}
